package d0908;
import java.util.*;
@SuppressWarnings("all")
public class List源码 {

    public static void main(String[] args) {
        // 数组实现  遍历快, 写入慢
        List<String> list1 = new ArrayList<>();
        // 链表实现  遍历慢, 写入快
        List<String> list2 = new LinkedList<>();
        // 矢量集合, 数组实现, 线程安全 ==> 性能低下
        List<String> list3 = new Vector<>();

        MyList<String> mylist = new MyList<>(10);
        mylist.add("张三");
        mylist.add("李四");
        mylist.add("王五");
        mylist.add("赵六");

        for (String name : mylist ) {
            System.out.println(name);
        }

    }

}

// T 泛型参数,  表示构建时出入的类型
class MyList<T> implements Iterable<T> {

    private Object[] objects;
    private int index = 0;

    public MyList(int size) {
        objects = new Object[size];
    }

    public void add(T t) {
        objects[index++] = t;
    }

    @Override
    public Iterator<T> iterator() {
        // 构建迭代器对象
        return new Iterator<T>() {
            private int index = 0;

            @Override
            public boolean hasNext() {
                return this.index < MyList.this.index;
            }

            @Override
            public T next() {
                return (T) MyList.this.objects[this.index++];
            }
        };
    }
}